<?php
class makehtml_articleAction extends security_login {
	
	function action(){
		ModuleLimit(__CLASS__);
		set_time_limit(0);
		$miceSession = new session();
		$miceSession->set('makehtml','miceCMS');

		global $conn;
		$category=new model_Category();
		$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 ORDER BY path ASC');
		$results=$category->doSelect();
		$allid='';

		echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		echo '<link href="'.MICE_URL.MICE_TPL.'/admin/css/default.css" rel="stylesheet" type="text/css">';
		echo '</head><body>';
		echo '<table width="99%" align="center" border="0" cellpadding="0" cellspacing="0">  <tr align="center">    <td height="5"></td>  </tr></table>';
		echo '<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0">  <tr>		<td width="860" align="center" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#D5D5D5">      <tr>        <td height="28" bgcolor="#F9F9F9">&nbsp;<span class="STYLE7"> 更新文档HTML文件</span></td>      </tr>      <tr>        <td height="28" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0"><form id="form1" name="form1" method="post" action="">';
		
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;选择分类</td><td height="28" class="bottomline">';
		echo "<select name='path' id='path'><option value='all'>所有分类</option>";
		foreach($results as $crs){
			$checked='';
			if($_POST['path']==$crs->getCategoryId()){
				$checked='selected';
			}
			$allid=$allid.','.$crs->getCategoryId();
			echo "<option value='".$crs->getCategoryId()."' ".$checked.">".StrRepeat("─",$crs->getDepth()-1).$crs->getTitle()."</option>";
		}
		echo "</select>";
		echo '</td></tr>';
		
		$checked='';
		$checked1='';
		if($_POST['parent']==1){
			$checked='checked';
		}elseif($_POST['parent']==2){
			$checked1='checked';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;是否更新上级分类的文档</td><td height="28" class="bottomline">';
		echo "<input name='parent' id='parent' type='radio' value='0' checked/>只更新此分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='1' ".$checked."/>更新此分类及此分类的所有上级分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='2' ".$checked1."/>更新此分类及此分类的所有下级分类";
		echo '</td></tr>';

		$checked2='';
		if($_POST['ishtml']){
			$checked2='checked';
		}

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;只更新未生成HTML的文档或做过修改的文档</td><td height="28" class="bottomline">';
		echo "<input name='ishtml' id='ishtml' type='radio' value='0' checked/>否&nbsp;&nbsp;<input name='ishtml' id='ishtml' type='radio' value='1' ".$checked2."/>是";
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '<input type="submit" name="Submit" value="开始生成HTML" /><br>(如果一次生成的分类过多,系统可能会较长时间没有反应,请耐心等待,不要做其它操作)';
		echo '</td></tr></form>';


		if($_POST['path']){
			if($_POST['path']=='all'){
				$Cids=explode(',',$allid);
			}else{
				if($_POST['parent']==1){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND category_id='.$_POST['path']);
					$CategoryRS0=$category0->doSelectOne();
					$Cids=explode(',',$CategoryRS0->getPath());
				}elseif($_POST['parent']==2){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND path like \'%,'.$_POST['path'].',%\'');
					$CategoryRS0=$category0->doSelect();
					
					$searchpath=','.$_POST['path'].',';
					$paths=$searchpath;
					foreach($CategoryRS0 as $category){
						$pathlen=strpos($category->getPath(),$searchpath);
						$paths.=substr($category->getPath(),$pathlen+strlen($searchpath));
					}
					$Cids=explode(',',$paths);
				}else{
					$Cids[0]=$_POST['path'];
				}
			}

			//print_r($Cids);
			$categoryname='';
			foreach($Cids as $CategoryId){
				if(is_numeric($CategoryId)){
					
					$category=new model_Category();
					$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND category_id='.$CategoryId);
					$CategoryRS=$category->doSelectOne();

					if($CategoryRS){

						if($CategoryRS->getSubtpl()){		//模板
							$tpl=$CategoryRS->getSubtpl();
						}else{
							$tpl='index/article.php';
						}

						//查找此分类的文章
						$ishtmlsql='';
						if($_POST['ishtml']){
							$ishtmlsql=' AND ishtml LIKE \'%,'.$CategoryId.',%\'';
						}else{
							$ishtmlsql=' AND category_id LIKE \'%,'.$CategoryId.',%\'';
						}
						$articlemode=new model_Article();
						$articlemode->setSubsection('WHERE isvisible=0'.$ishtmlsql);
						$ArticleRS=$articlemode->doSelect();

						$categoryname.='<br>'.$CategoryRS->getTitle().'&nbsp;&nbsp;栏目生成HTML的文档数：'.sizeof($ArticleRS);

						foreach($ArticleRS as $Article){
							$content='';
							if($Article->getUrl()){
								//跳转
								$content='<html><body><form action="'.MiceRestoreQuote('micefun',$Article->getUrl()).'"  method="post" id="link" name="link"  target="_blank"  style="display:none;"></form><Script LANGUAGE="JavaScript">document.getElementById("link").submit();history.back(-1);</script></body></html>';
							}

							if($Article->getGroupId()<0){
								$gid=$CategoryRS->getGroupId();
							}else{
								$gid=$Article->getGroupId();
							}
							$miceSession = new session();
							$limit='';
							if($gid>0){			
								$limit='<body><iframe src="'.MICE_URL.'index.php/limit/'.$Article->getArticleId().'/'.$gid.'/'.$Article->getPointnum().'"  style="width:0px; height:0px;" frameborder="0"></iframe>';
							}else{
								$limit='<body><iframe src="'.MICE_URL.'index.php/limit/'.$Article->getArticleId().'/0/'.$Article->getPointnum().'"  style="width:0px; height:0px;" frameborder="0"></iframe>';
							}

							//作者
							$user=new model_User();
							$user->setSubsection('where user_id='.$Article->getUserId());
							$userrs=$user->doSelectOne();
							$wname=$userrs->getLoginname();

							$smarty = new Smarty();
							$smarty->load_filter('pre','micePreCompileIndex');
							$smarty->assign('CategoryRS',$CategoryRS);
							$smarty->assign('ArticleRS',$Article);
							$smarty->assign('CategoryId',$CategoryId);
							$smarty->assign('ArticleId',$Article->getArticleId());
							$smarty->assign('wname',$wname);

							//将内容写入至一个静态文件
							if($content==''){
								$content = $smarty->fetch($tpl);
								if($limit!=''){
									$content = str_replace("<body>", $limit, $content);
								}
							}
							$htmlfn=GetHtmlPathArticle($CategoryRS->getCategoryId(),$CategoryRS->getFilename(),$CategoryRS->GetArticlerule(),$Article->getArticleId(),$Article->getFilename(),$Article->getCreatedat());
							if(!$htmlfn){
								echo '<tr><td height="28" class="bottomline" width="300">&nbsp;生成结果</td><td height="28" class="bottomline"><font color="red">生成错误:请检查html目录是否有写入权限</font><br></td></tr></form>';
								echo '</table></td></tr></table></td></tr></table><br></body></html>';
								die();
							}
							$fp = fopen(MICE_DIR.$htmlfn, 'w'); 
							fwrite($fp, $content); 
							fclose($fp);
							
							//对已生成html的栏目下所有文章做标记
							$articlemode2=new model_Article();
							$articlemode2->setSubsection('WHERE article_id='.$Article->getArticleId());
							$ArticleRS2=$articlemode2->doSelectOne();

							if($ArticleRS2){
								$newihsthml=str_replace(','.$CategoryId.',', ',', $Article->getIshtml());
								//echo $CategoryId,'----',$Article->getArticleId(),'----',$Article->getIshtml(),'----',$newihsthml,'<br>';
								$articlemode2->setIshtml($newihsthml);
								$articlemode2->doUpdate();
							}
						}
					}
				}
			}
		}
		if(!$categoryname){
			$categoryname='本次操作没有更新任何分类下的文档';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;生成结果</td><td height="28" class="bottomline">';
		echo $categoryname;
		echo '<br></td></tr></form>';
		echo '</table></td></tr></table></td></tr></table><br></body></html>';
		$miceSession->set('makehtml','');
	}
}
?>